﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DNNspot.LiveChat.Common;
using DNNspot.LiveChat.DataModel;
using DotNetNuke.Common;

namespace DNNspot.LiveChat.Admin
{
    public partial class EditCategory : ModuleBase
    {
        private int? _categoryId;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadSettings();
                LoadControls();
            }

            LoadCategoryId();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Redirect();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            Delete();
            Redirect();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Save();
            Redirect();
        }

        private void Delete()
        {
            var categories = new CategoryCollection();
            categories.LoadAll();

            foreach (Category c in categories)
            {
                if (c.IsChildCategoryOf(_categoryId))
                    c.MarkAsDeleted();
            }

            categories.Save();

            var category = Category.Load(_categoryId);
            category.MarkAsDeleted();
            category.Save();
        }

        private void LoadCategoryDropDownList()
        {
            var categories = new CategoryCollection();
            categories.LoadAll();

            foreach (Category category in categories.SortByParentTree())
                if(!category.IsChildCategoryOf(_categoryId) && category.CategoryId != _categoryId)
                    ddlCategories.Items.Add(new ListItem(
                        category.GetTreeName(), 
                        category.CategoryId.ToString()));

            ddlCategories.Items.Insert(0, new ListItem("None", ""));
        }

        private void LoadCategoryId()
        {
            _categoryId = null;

            if (Convert.ToInt32(hidCategoryId.Value) > -1)
                _categoryId = Convert.ToInt32(hidCategoryId.Value);
        }

        private void LoadControls()
        {
            LoadCategoryId();

            var category = Category.Load(_categoryId);

            LoadCategoryDropDownList();

            if(ddlCategories.Items.FindByValue(category.ParentCategoryId.ToString()) != null)
                ddlCategories.Items.FindByValue(category.ParentCategoryId.ToString()).Selected = true;

            txtCategoryName.Text = category.Name;

            if(_categoryId != null)
                btnDelete.Visible = true;
        }

        private void LoadSettings()
        {
            _categoryId = Convert.ToInt32(Request.QueryString[Params.CategoryId]);
            hidCategoryId.Value = _categoryId.ToString();
        }

        private void Redirect()
        {
            Response.Redirect(Globals.NavigateURL(TabId));
        }

        private void Save()
        {
            int? parentCategoryId = null;

            if(ddlCategories.SelectedValue != "")
                parentCategoryId = Convert.ToInt32(ddlCategories.SelectedValue);

            var category = Category.Load(_categoryId);
            category.PortalId = PortalId;
            category.ParentCategoryId = parentCategoryId;
            category.Name = txtCategoryName.Text;
            category.Save();
        }
    }
}